package com.zlm.user.emp.util;

import com.zlm.common.constants.CommonConstants;
import com.zlm.common.constants.EmpResumeConstants;
import com.zlm.common.constants.UserConstants;
import com.zlm.common.dto.userEmp.EmpInfoParamsDto;
import com.zlm.common.dto.userEmp.EmpResumeResponseDto;
import com.zlm.common.dto.userEmp.SysEmpDto;
import com.zlm.common.entity.EmpResumeDO;
import com.zlm.common.entity.EmpUserEducationDO;
import com.zlm.common.entity.EmpUserExperienceDO;
import com.zlm.common.entity.SysTimeSectionDO;
import com.zlm.common.enums.*;
import com.zlm.common.util.DateTimeUtils;
import com.zlm.user.emp.dao.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 用户工具类
 * @author xhj
 */
@Component
public class EmpUtils {

    @Autowired
    private SysEmpDao sysEmp;
    private static SysEmpDao sysEmpDao;

    @Autowired
    private EmpResumeDao empResume;
    private static EmpResumeDao empResumeDao;

    @Autowired
    private SysIndustryDao sysIndustry;
    private static SysIndustryDao sysIndustryDao;

    @Autowired
    private SysTimeSectionDao sysTimeSection;
    private static SysTimeSectionDao sysTimeSectionDao;

    @Autowired
    private EmpUserEducationDao empUserEducation;
    private static EmpUserEducationDao empUserEducationDao;

    @Autowired
    private EmpUserExperienceDao empUserExperience;
    private static EmpUserExperienceDao empUserExperienceDao;

    @PostConstruct
    public void init() {
        sysEmpDao = this.sysEmp;
        empResumeDao = this.empResume;
        sysIndustryDao = this.sysIndustry;
        sysTimeSectionDao = this.sysTimeSection;
        empUserEducationDao = this.empUserEducation;
        empUserExperienceDao = this.empUserExperience;
    }

    /**
     * 获取个人简历
     * @param empId
     * @return
     */
    public static EmpResumeResponseDto getResume(Integer empId) {
        SysEmpDto sysEmpDto = sysEmpDao.queryEmpById(empId);
        if (sysEmpDto == null) {
            return null;
        }
        sysEmpDto.setAvatar(sysEmpDto.getAvatar() == null ? "" : CommonConstants.IMG_URL.concat(sysEmpDto.getAvatar()));
        sysEmpDto.setEmpName(sysEmpDto.getEmpName()==null ? UserConstants.USER_DEFAULT_NAME : sysEmpDto.getEmpName());
        sysEmpDto.setEducationName(EducationEnum.getNameByCode(sysEmpDto.getEducation()));
        sysEmpDto.setIsWorkName(JobHuntingStatusEnum.getNameByCode(sysEmpDto.getIsWork()));

        EmpResumeDO empResumeDO = empResumeDao.queryResumeById(empId);
        List<Map<String,Object>> industryInfo = null;
        if (null != empResumeDO) {
            if (null != empResumeDO.getResumeStyle()) {
                empResumeDO.setResumeStyleName(WorkTypeEnum.getNameByCode(empResumeDO.getResumeStyle()));
            }
            empResumeDO.setSalaryName(SalaryRangeEnum.getNameByCode(empResumeDO.getSalary()));
            //查询期望行业
            List<Integer> industryList = new ArrayList<>();
            if (empResumeDO.getIndustry() != null && !"".equals(empResumeDO.getIndustry())) {
                industryList.add(empResumeDO.getIndustry());
            }
            if (empResumeDO.getIndustry1() != null && !"".equals(empResumeDO.getIndustry1())) {
                industryList.add(empResumeDO.getIndustry1());
            }
            if (empResumeDO.getIndustry2() != null && !"".equals(empResumeDO.getIndustry2())) {
                industryList.add(empResumeDO.getIndustry2());
            }
            if (!industryList.isEmpty()){
                List<Map<String,Object>> industryInfoResult = sysIndustryDao.queryIndustryList(industryList);
                if (industryInfoResult.size() > 0) {
                    industryInfo = industryInfoResult;
                }
            }
            SysTimeSectionDO sysTimeSection = new SysTimeSectionDO();
            sysTimeSection.setResumeId(empResumeDO.getResumeId());
            sysTimeSection.setAlive(1);
            //工作时间
            sysTimeSection.setIdentitys(1);
            List<SysTimeSectionDO> sysTimeSections = sysTimeSectionDao.queryTimeSection(sysTimeSection);
            //培训时间
            sysTimeSection.setIdentitys(2);
            List<SysTimeSectionDO> trainTimeSections = sysTimeSectionDao.queryTimeSection(sysTimeSection);
            empResumeDO.setSysTimeSections(sysTimeSections);
            empResumeDO.setTrainTimeSections(trainTimeSections);
        }else {
            empResumeDO = new EmpResumeDO();
        }
        //获取用户教育经验信息
        List<EmpUserEducationDO> empUserEducationList = empUserEducationDao.queryUserEducation(empId);
        for (EmpUserEducationDO empUserEducationDO : empUserEducationList) {
            empUserEducationDO.setDegreeName(EducationEnum.getNameByCode(empUserEducationDO.getDegree()));
        }
        //获取用户工作经验信息
        List<EmpUserExperienceDO> empUserExperienceList = empUserExperienceDao.queryUserExperience(empId);
        for (EmpUserExperienceDO empUserExperienceDO : empUserExperienceList) {
            if (null != empUserExperienceDO.getScale()) {
                empUserExperienceDO.setScaleName(CompanyScaleEnum.getNameByCode(empUserExperienceDO.getScale()));
            }
        }
        if (empUserExperienceList.size() > 0) {
            Integer year = DateTimeUtils.getDiffYear(
                    empUserExperienceList.get(empUserExperienceList.size()-1).getStartDate(), DateTimeUtils.getDate());
            sysEmpDto.setExperience(year);
        }

        EmpResumeResponseDto empResumeDto = new EmpResumeResponseDto();
        empResumeDto.setSysEmpDto(sysEmpDto);
        empResumeDto.setIndustryInfo(industryInfo);
        empResumeDto.setEmpResumeDO(empResumeDO);
        empResumeDto.setEmpUserEducations(empUserEducationList);
        empResumeDto.setEmpUserExperiences(empUserExperienceList);
        return empResumeDto;
    }

    /**
     * 更新简历完善度
     * @param empId
     */
    public static void updateEmpPerfect(Integer empId) {
        EmpResumeResponseDto o = getResume(empId);
        Integer score = 0;
        score = getScore(o, score);
        EmpInfoParamsDto empDto = new EmpInfoParamsDto();
        empDto.setEmpId(empId);
        empDto.setIsPerfect(score);
        sysEmpDao.saveEmpInfo(empDto);
    }

    /**
     * 获取得分
     * @param o
     * @param socre
     * @return
     */
    private static Integer getScore(Object o, Integer socre) {
        if (o != null) {
            Field[] fields = o.getClass().getDeclaredFields();
            for (Field name : fields) {
                String n = name.getName();
                Object v = getFieldValueByName(n, o);
                if (SysEmpDto.class.isInstance(v) || EmpResumeDO.class.isInstance(v)) {
                    socre = getScore(v, socre);
                    continue;
                }
                //对象为空或者列表长度为0时，不计分
                if (v == null || StringUtils.isEmpty(v.toString()) ||
                        (ArrayList.class.isInstance(v) && ((ArrayList) v).size() == 0)) {
                    continue;
                }
                socre = socre + EmpResumeConstants.getScore(n);
            }
        }

        return socre;
    }

    /**
     * 根据属性名获取属性值
     * */
    private static Object getFieldValueByName(String fieldName, Object o) {
        try {
            String firstLetter = fieldName.substring(0, 1).toUpperCase();
            String getter = "get" + firstLetter + fieldName.substring(1);
            Method method = o.getClass().getMethod(getter, new Class[] {});
            Object value = method.invoke(o, new Object[] {});
            return value;
        } catch (Exception e) {
            return null;
        }
    }
}
